package com.hqyj.system.service.impl;

import com.hqyj.system.mapper.SysUserMapper;
import com.hqyj.system.model.SysUser;
import com.hqyj.system.service.SysUserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import com.hqyj.system.model.SysUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class SysUserServiceImpl implements SysUserService {
    private static SysUserMapper sysUserMapper;
    private static SqlSession sqlSession;

    @Override
    public SysUser querySysUserByNameAndPassword(SysUser sysUser) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建sqlsessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);

        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //###################业务###################
        SysUser sysUser1=sysUserMapper.querySysUserByNameAndPassword(sysUser);
        //###################业务###################
        sqlSession.commit();
        return sysUser1;
    }

    @Override
    public List<SysUser> queryAllSysUser() throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建sqlsessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);

        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //###################业务###################
        List<SysUser> sysUser_db=sysUserMapper.queryAllSysUser();
//        提交事务
        sqlSession.commit();
        return sysUser_db;
    }
    @Override
    public int addSysUserBySysUser(SysUser sysUser) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //############################ 业务 ################################
        int n = sysUserMapper.insertSelective(sysUser);


        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }

    @Override
    public int deleteSysUserByUserId(int userId) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //############################ 业务 ################################
        int n = sysUserMapper.deleteByPrimaryKey(userId);


        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }

    @Override
    public SysUser querySysUserByUserId(int userId) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //############################ 业务 ################################
        SysUser sysUser_db = sysUserMapper.selectByPrimaryKey(userId);

        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return sysUser_db;
    }

    @Override
    public int updateSysUserBySysUser(SysUser sysUser) throws IOException {
        //1.读取配置文件（mybatis-config.xml）
        InputStream iStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
        //2.创建SqlSessionFactoryBuilder构造器，得到SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(iStream);
        //3.获取Session对象
        sqlSession = sqlSessionFactory.openSession();
        //4.获取mapper代理对象
        sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        //############################ 业务 ################################
        int n = sysUserMapper.updateByPrimaryKeySelective(sysUser);


        //############################ 业务 ################################
        //提交事务
        sqlSession.commit();
        return n;
    }

}
